<template>
  <!-- 设备控制参数表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="版本号" prop="version">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.version"
              placeholder="请输入版本号"
              clearable
            />

            <span v-else>{{ formData.version }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备主键" prop="equipId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipId"
              placeholder="请输入设备主键"
              clearable
            />

            <span v-else>{{ formData.equipId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型主键" prop="equipTypeId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipTypeId"
              placeholder="请输入设备类型主键"
              clearable
            />

            <span v-else>{{ formData.equipTypeId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数名" prop="parameterName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.parameterName"
              placeholder="请输入参数名"
              clearable
            />

            <span v-else>{{ formData.parameterName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" prop="url">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.url"
              placeholder="请输入地址"
              clearable
            />

            <span v-else>{{ formData.url }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="描述" prop="describe">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.describe"
              placeholder="请输入描述"
              clearable
            />

            <span v-else>{{ formData.describe }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据类型" prop="parameterType">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.parameterType"
              placeholder="请输入数据类型"
              clearable
            />

            <span v-else>{{ formData.parameterType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示序号" prop="sort">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.sort"
              placeholder="请输入显示序号"
              clearable
            />

            <span v-else>{{ formData.sort }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用" prop="enabled">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.enabled"
              placeholder="请输入是否启用"
              clearable
            />

            <span v-else>{{ formData.enabled }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />

            <span v-else>{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否删除" prop="deleted">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.deleted"
              placeholder="请输入是否删除"
              clearable
            />

            <span v-else>{{ formData.deleted }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间" prop="createTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createTime"
              placeholder="请输入创建时间"
              clearable
            />

            <span v-else>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人主键" prop="createdUserId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserId"
              placeholder="请输入创建人主键"
              clearable
            />

            <span v-else>{{ formData.createdUserId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人姓名" prop="createdUserName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserName"
              placeholder="请输入创建人姓名"
              clearable
            />

            <span v-else>{{ formData.createdUserName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新时间" prop="lastUpdateTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdateTime"
              placeholder="请输入更新时间"
              clearable
            />

            <span v-else>{{ formData.lastUpdateTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人主键" prop="lastUpdatedUserId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserId"
              placeholder="请输入更新人主键"
              clearable
            />

            <span v-else>{{ formData.lastUpdatedUserId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人姓名" prop="lastUpdatedUserName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserName"
              placeholder="请输入更新人姓名"
              clearable
            />

            <span v-else>{{ formData.lastUpdatedUserName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "ADMINSCAEQUIPCONTROLPARAMADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  version: [],
  equipId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  equipTypeId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  parameterName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  url: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  describe: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  parameterType: [
    { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" },
  ],
  sort: [],
  enabled: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  deleted: [],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  equipId: "",
  equipTypeId: "",
  parameterName: "",
  url: "",
  describe: "",
  parameterType: "",
  sort: "",
  enabled: "",
  remarks: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        equipId,
        equipTypeId,
        parameterName,
        url,
        describe,
        parameterType,
        sort,
        enabled,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        equipId,
        equipTypeId,
        parameterName,
        url,
        describe,
        parameterType,
        sort,
        enabled,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.equipId = "";
  formData.equipTypeId = "";
  formData.parameterName = "";
  formData.url = "";
  formData.describe = "";
  formData.parameterType = "";
  formData.sort = "";
  formData.enabled = "";
  formData.remarks = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 设备控制参数保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/iotadmin/scaequipcontrolparam/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
